home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / Softshoe / Lisa's Portable Parts / Arrays / ConstArrayOf.h < prev   
Encoding:
Text File  |  2000-06-23  |  3.9 KB  |  136 lines

  1. // ConstArrayOf.h
  2.  
  3. #ifndef ConstArrayOf_h
  4. #define ConstArrayOf_h
  5.  
  6. #ifndef Integers_h
  7. #include "Integers.h"
  8. #endif
  9. #ifndef DebugMessage_h
  10. #include "DebugMessage.h"
  11. #endif
  12. #ifndef Assert_h
  13. #include "Assert.h"
  14. #endif
  15. #ifndef Range_h
  16. #include "Range.h"
  17. #endif
  18.  
  19. // The includes above have been flattened so that weak compilers
  20. // can cope better.
  21.  
  22. template < class Element >
  23. class ConstArrayOf
  24.   {
  25.     public:
  26.         typedef ConstArrayOf< Element > ConstArrayType;
  27.         typedef int32 (*Comparison)( const Element&, const Element& );
  28.  
  29.     private:
  30.         const Element *start;
  31.         uint32 length;
  32.     
  33.     public:
  34.         ConstArrayOf()
  35.           : start( 0 ),
  36.             length( 0 )
  37.           {}
  38.         
  39.         ConstArrayOf( const Element *theStart, uint32 theLength )
  40.           : start( theStart ),
  41.              length( theLength )
  42.           { Assert( theStart != 0 || length == 0 ); }
  43.  
  44.         const Element *Start() const                        { return start; }
  45.         const Element *End() const                            { return start + length; }
  46.  
  47.         uint32 Length() const                                { return length; }
  48.         const URange32 Range() const                        { return URange32( 0, Length() ); }
  49.                 
  50.         uint32 BoundedLength( uint32 bound ) const
  51.             { return ( length <= bound ) ? length : bound; }
  52.  
  53.         void LimitLength( uint32 limit )                    { length = BoundedLength( limit ); }
  54.         
  55.         bool IsEmpty() const                                    { return length == 0; }
  56.         bool Null() const                                        { return start == 0; }
  57.         
  58.         const Element& operator[]( uint32 i ) const    { Assert( i < length ); return start[i]; }
  59.         
  60.         inline const ConstArrayType Head( uint32 position ) const;
  61.         inline const ConstArrayType Tail( uint32 position ) const;
  62.         inline const ConstArrayType Middle( URange32 ) const;
  63.         
  64.         void Truncate( uint32 position );                // *this = Head( *this, position );
  65.         inline void Shorten( uint32 amount );            // *this = Tail( *this, amount )
  66.  
  67.         void Append( ConstArrayType tail );
  68.         void Prepend( ConstArrayType head );
  69.         
  70.         void operator+=( ConstArrayType tail )            { Append( tail ); }
  71.         const ConstArrayType operator+( ConstArrayType tail ) const;
  72.         
  73.         bool Precedes( ConstArrayType r ) const        { return End() == r.start; }
  74.         bool Follows( ConstArrayType r ) const            { return start == r.End(); }
  75.         
  76.         bool Contains( const Element *e ) const        { return start <= e && e < End(); }
  77.         
  78.         bool IsHeadOf( ConstArrayType r ) const;
  79.         bool IsTailOf( ConstArrayType r ) const;
  80.         
  81.         bool IsSortedBy( Comparison ) const;
  82.         
  83.         // These operators express the containment ordering
  84.         bool operator==( ConstArrayType r ) const        { return start == r.start && length == r.length; }
  85.         bool operator!=( ConstArrayType r ) const        { return start != r.start || length != r.length; }
  86.         bool operator<=( ConstArrayType r ) const;
  87.         bool operator>=( ConstArrayType r ) const        { return r <= *this; }
  88.         bool operator<( ConstArrayType r ) const;
  89.         bool operator>( ConstArrayType r ) const        { return r < *this; }
  90.         
  91.         bool Touches( ConstArrayType r ) const;
  92.         const ConstArrayType operator&( ConstArrayType r ) const;
  93.         const ConstArrayType operator|( ConstArrayType r ) const;
  94.         void operator&=( ConstArrayType r )                { *this = *this & r; }
  95.         void operator|=( ConstArrayType r )                { *this = *this | r; }
  96.   };
  97.  
  98. template < class Element >
  99. inline void ConstArrayOf<Element>::Shorten( uint32 amount )
  100.   {
  101.     Assert( !Null() );
  102.     Assert( amount <= length );
  103.     start += amount;
  104.     length -= amount;
  105.   }
  106.  
  107. template < class Element >
  108. inline const ConstArrayOf<Element> ConstArrayOf<Element>::Head( uint32 size ) const
  109.   {
  110.     Assert( !Null() );
  111.     Assert( size <= length );
  112.     return ConstArrayType( start, size );
  113.   }
  114.  
  115. template < class Element >
  116. inline const ConstArrayOf<Element> ConstArrayOf<Element>::Tail( uint32 position ) const
  117.   {
  118.     Assert( !Null() );
  119.     Assert( position <= length );
  120.     return ConstArrayType( start + position, length - position );
  121.   }
  122.  
  123. template < class Element >
  124. inline const ConstArrayOf<Element> ConstArrayOf<Element>::Middle( URange32 range ) const
  125.   {
  126.     Assert( !Null() );
  127.     Assert( range.End() <= length );
  128.     return ConstArrayType( start + range.Start(), range.Length() );
  129.   }
  130.  
  131. #ifndef ConstArrayOf_cp
  132. #include "ConstArrayOf.cp"
  133. #endif
  134.  
  135. #endif
  136.